<?php

namespace Home\Controller;

class OrderController extends CommonController {

    public function add() {
        $sendtime = I("post.sendtime");
        $data['remark'] = I("post.message"); //留言
        if ($sendtime > 0) {
            $hour = intval($sendtime / 60);
            $minites = intval($sendtime % 60);
            if(strlen($minites) == 1){
                $minites = "0".$minites;
            }
            $data['remark'] .= "&nbsp;" . $hour . ":" . $minites . "送出";
        }
        $area_id = I("post.area_id", 0, 'int');
        $addr = M("addr")->field("truename,addr,phone,phone2")->where("id = " . $area_id . " AND uid = " . session("userid") . "")->find();
        $data['truename'] = $addr['truename'];
        $data['addr'] = $addr['addr'];
        $data['phone'] = $addr['phone'];
        $data['phone2'] = $addr['phone2'];
        $data['uid'] = session("userid");
        $carts = json_decode(cookie("cart"), true);
        if (empty($carts)) {
            echo "操作有误，请重新下单！";
            exit;
        }
        $totalMoney = 0;
        $discount = 0;
        foreach ($carts as $k => $v) {
            $info = M("goods")->field("price,sid,discount")->where("id = " . $v['id'] . "")->find();
            $totalMoney += $info['price'] * $v['num'];
            $discount += $info['discount'] * $v['num'];
            $sid = $info['sid'];
        }

        $data['sid'] = $sid;
        $shop = M("shop")->field("sales_cut,sales_full,business_time,weeks,is_busy,startprice")->where("id = " . $sid . "")->find();
        $shopState = getShopState($shop['business_time'], $shop['weeks'], $shop['is_busy']);
        if ($shopState != '') {
            echo "该餐厅" . $shopState;
            exit;
        }
        if ($shop['startprice'] > $totalMoney) {
            echo "该餐厅起步价为" . $shop['startprice'] . "元";
            exit;
        }
        if ($totalMoney > $shop['sales_full']) {//若订单额大于满价
            $data['realMoney'] = $totalMoney - $shop['sales_cut'] - $discount;
        } else {
            $data['realMoney'] = $totalMoney - $discount;
        }
        $data['totalMoney'] = $totalMoney;
        $data['ordernum'] = getOrdernum();
        $data['addtime'] = time();
        $data2['pid'] = M("order")->add($data);
//        echo  M("order")->getlastsql();
        if ($data2['pid'] > 0) {
            foreach ($carts as $k => $v) {
                $info = M("goods")->field("price,discount,name")->where("id = " . $v['id'] . "")->find();
                $data2['gprice'] = $info['price'] - $info['discount'];
                $data2['gname'] = $info['name'];
                $data2['gid'] = $v['id'];
                $data2['gnum'] = $v['num'];
                M("order_goods")->add($data2);
            }
            cookie("cart", null);
            echo $data2['pid'];
            sendEmailOrder($data2['pid']);
            sendMobileOrder($data2['pid']);
        }
    }

    public function success() {
        $orderid = I("get.id");
        $order = M("order")->field("phone,sid")->where("id = " . $orderid . "")->find();
        $order['sendtime'] = getSingleField($order['sid'], 'shop', 'sendtime', '');
        if($order['sendtime'] == 0){
            $order['sendtime'] = 30;
        }
        $this->assign("order", $order);
        $this->assign("ctitle", "订单提交成功");
        $this->display();
    }

    public function find() {
        $this->assign("ctitle", "催 单");
        $this->display();
    }
   

}
